$(function(){
    $('#login').validate({
        rules:{
            tel: {
                required: true,
                rangelength: [11,11],
                checkPhone:true
            },
            pass: {
                required: true,
                rangelength: [8,20],
                checkPassword:true
            },
            yzm: {
                required: true,
                rangelength: [4,4],
                checkGraphCode:true
            }
        },
        messages:{
            tel: {
                required: '用户名不能为空',
                rangelength: '请输入正确的用户名'
            },
            pass: {
                required: '密码不能为空',
                rangelength: '密码长度为8-20位'
            },
            yzm: {
                required: '验证码不能为空',
                rangelength: '请输入正确的验证码'
            }
        },
        debug: true,
        focusInvalid: false,
        onkeyup: false,
        success: 'valid',
        submitHandler:function(){
            $('#login').find(":submit").attr("disabled", true);
            var username = $('#tel').val();
            var userpwd = $('#pass').val();
            var verifyCode = $('#yzm').val();
            $.ajax({
                url: '/login',
                data:{
                    username: username,
                    userpwd: userpwd,
                    verifyCode: verifyCode
                },
                typeDate: 'json',
                type: 'post',
                success:function(data){
                    if(data.success){
                        window.location.href= '/';
                    }else{
                        if(data.code=='1105'){
                            $.graphcode.refresh('.img-code');
                        }
                        $.errorHandler('#login','',data.msg);
                    }
                    $('#login').find(":submit").attr("disabled", false);
                },
                error:function(error){}
            })
        }
    });
});